package com.ws.yj.web;

import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import okhttp3.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/login")
public class KyLoginController {
    public final static String Success = "success";
    OkHttpClient client=new OkHttpClient();
//    @RequestMapping("/cookie")
//    public void getCookie(HttpServletRequest request, HttpServletResponse response){
//       Request getCookie = new Request.Builder()
//       .url("https://coyee.com")
//       .build();
//       Response cookieResponse = null;
//        try {
//            cookieResponse = client.newCall(getCookie).execute();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        if (cookieResponse.isSuccessful()){
//
//            List<String> cookie=cookieResponse.headers("Cookie");
//            request.getSession(true).setAttribute("cookie",cookie);
//        }
//    }


    @RequestMapping("/doLogin")
    @ResponseBody
    public String  login(HttpServletRequest request,@RequestParam(name = "uid",defaultValue = "18224072177") String name, @RequestParam(name="pwd",defaultValue = "qq18224072177") String password) throws IOException {
        long timestamp = System.currentTimeMillis();
        RequestBody params=new FormBody.Builder()
                .add("timestamp", String.valueOf(timestamp))
                .add("uid",name)
                .add("pwd",password)
                .build();
        Request login=new Request.Builder()
                .url("https://coyee.com/action/user/signin")
                .addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                .addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Referer","https://coyee.com/signin")
                .addHeader("Origin","https://coyee.com")
                .post(params)
                .build();
        Response result=null;
        Response cookieResponse = client.newCall(login).execute();
        List<String> cookie = cookieResponse.headers("Set-Cookie");
        request.getSession().setAttribute("cookies",String.valueOf(cookie));
        System.out.println(String.valueOf(cookie));
        try {
            result = client.newCall(login).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (result.isSuccessful()) {
            try {
                System.out.println(result.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return JSON.toJSONString(Success);
    }
    @RequestMapping("/voice")
    @ResponseBody
    public String voice (HttpServletRequest request,@RequestParam(name = "user",defaultValue = "12050") String username,@RequestParam(name = "userCode",defaultValue = "rmoLVQCWznhge5bFLER9P6owjEQMtCRaGKzD2eK4") String userCode,String memo){
        long timestamp = System.currentTimeMillis();
        String[] s = new String[]{":bowtie:",":smile:",":laughing:",":blush:",":smiley:",":relaxed:",":smirk:",":heart_eyes:",":kissing_heart:",":kissing_closed_eyes:",":flushed:",":relieved:",":satisfied:",":grin:",":wink:",":stuck_out_tongue_winking_eye:",":stuck_out_tongue_closed_eyes:",":grinning:",":kissing:",":kissing_smiling_eyes:",":stuck_out_tongue:",":sleeping:",":worried:",":frowning:",":anguished:",":open_mouth:",":grimacing:",":confused:",":hushed:",":expressionless:",":unamused:",":sweat_smile:",":sweat:",":weary:",":pensive:",":disappointed:",":confounded:",":fearful:",":cold_sweat:",":persevere:",":cry:",":sob:",":joy:",":astonished:",":scream:",":tired_face:",":angry:",":rage:",":triumph:",":sleepy:",":yum:",":mask:",":sunglasses:",":dizzy_face:",":imp:",":smiling_imp:",":neutral_face:",":innocent:",":alien:",":heart:",":cupid:",":sparkles:",":boom:",":exclamation:",":question:",":zzz:",":thumbsup:",":-1:",":ok_hand:",":punch:",":fist:",":v:",":open_hands:",":point_up:",":point_down:",":point_left:",":point_right:",":raised_hands:",":pray:",":clap:",":muscle:",":metal:",":kiss:",":trollface:"};
        Random r = new Random();
        int s1 = r.nextInt(s.length);
        RequestBody params=new FormBody.Builder()
                .add("timestamp",String.valueOf(timestamp))
                .add("user",username)
                .add("userCode",userCode)
                .add("memo",s[s1])
                .build();
        //System.out.println(s[s1]);
        //String cookie = "";
        System.out.println("cookies："+request.getSession().getAttribute("cookies"));
        Request voice=new Request.Builder()
                .url("https://coyee.com/action/tweet/add")
                .addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                .addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Referer","https://coyee.com/zhi")
                .addHeader("Cookie", (String) request.getSession().getAttribute("cookies"))
                .post(params)
                .build();

        Response result = null;
        try {
            result = client.newCall(voice).execute();
        }catch (IOException e){
            e.printStackTrace();
        }
        if(result.isSuccessful()){
            try {
                System.out.println(result.body().string());
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return Success;
    }
}
